import OrderDetailView from '../../views/OrderDetailView.vue'

export default {
    name: "order",
    data() {
        return {
            detailList: [],
            payList: [],
            confirmList: [],
            dialogVisible: false,
            showDetail: false,
            isComment: true,         //是否显示评价输入内容 
            commentContent: '',      //评价的内容
            activeName: "first",
            orders: []              //全部订单数据;
        }
    },
    computed: {
        noPay() {
            return this.orders.filter(order => {
                return order.state == 1;
            });
        },
        noConfirm() {
            return this.orders.filter(order => {
                return order.state == 2;
            });
        },
    },
    created() {
        this.getOrdersAll()
    },
    methods: {
        // 获取所有订单
        getOrdersAll() {
            this.axios.defaults.headers.common['Authorization'] = localStorage['token'];
            this.axios.get('api/getOrders')
                .then(res => {
                    this.orders = res.data.data;
                    this.getOrders()
                })
        },
        handleClose(done) {
            done();
        },
        // 订单分类
        getOrders() {
            const newArr = this.orders.reduce((acc, cur) => {
                if (!acc[cur.state]) {
                    acc[cur.state] = [];
                }
                acc[cur.state].push(cur);
                return acc;
            }, {});
            this.payList = newArr['1']
            this.confirmList = newArr['2']
        },
        // 时间格式转换
        timeTrans(time, type) {
            let date = new Date(time)
            date = date.toJSON();
            if (type === 1) {
                date = date.substring(0, 10)
            }
            else {
                date = date.substring(0, 19).replace('T', ' ')
            }
            return date
        },
        handleclick(tab, event) {
        },
        // 查看订单详情
        getDetail(e) {
            console.log('e:', e)
            this.dialogVisible = true
            this.showDetail = !this.showDetail
            this.detailList = e
        },
        // 去支付
        pay(id) {
            this.axios.defaults.headers.common['Authorization'] = localStorage['token']
            this.axios.put('api/editOrder', { params: { orderId: id, state: 2 } })
                .then(res => {
                    this.getOrdersAll()
                    this.$message({
                        type: 'success',
                        duration: 1000,
                        message: '支付成功'
                    });
                })
        },
        indexMethod(index) {
            return index + 1;
        },
        // 确认收货
        confirm(id) {
            console.log('id:', id)
            this.axios.defaults.headers.common['Authorization'] = localStorage['token']
            this.axios.put('api/editOrder', { params: { orderId: id, state: 3 } })
                .then(res => {
                    this.getOrdersAll()
                    this.$message({
                        type: 'success',
                        duration: 1000,
                        message: '收货成功'
                    });
                })
        },
        // 删除
        delOrder(e) {
            console.log('id:', e)
            this.$confirm('订单删除后不可恢复, 是否继续?', '提示', {
                confirmButtonText: '确定',
                cancelButtonText: '取消',
                type: 'warning'
            }).then(() => {
                this.axios.defaults.headers.common['Authorization'] = localStorage['token']
                this.axios.delete('api/delOrders', { params: { id: e } })
                    .then(res => {
                        this.getOrdersAll()
                        this.$message({
                            type: 'success',
                            duration: 1000,
                            message: '删除成功'
                        });
                    })
            }).catch(() => {
            });
        },
        editOrder(id, state) {
            console.log('id:', id)
            // let messages = ['支付成功', '确认成功', '评价成功!']
            this.axios.defaults.headers.common['Authorization'] = localStorage['token']
            // this.axios.put('api/editOrder', { params: { orderId: id, state: 3 } })
            this.axios.put('api/editOrder', { params: { id, state: 3 } })
                .then(res => {
                    // this.$message({
                    //     type: 'success',
                    //     duration: 1000,
                    //     message: '收货成功'
                    // });
                    // this.$message(messages[state - 1])
                    // this.$router.go(0)
                })
        }
    },
    components: { OrderDetailView }
}